package cn.sinata.wanglin.mine.activity;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.facebook.drawee.view.SimpleDraweeView;
import com.xilada.xldutils.activitys.RecyclerListActivity;
import com.xilada.xldutils.adapter.BaseRecyclerAdapter;
import com.xilada.xldutils.adapter.util.ViewHolder;
import com.xilada.xldutils.utils.PermissionManager;

import java.util.ArrayList;

import cn.sinata.wanglin.R;
import cn.sinata.wanglin.model.SignupUserBean;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;
import cn.sinata.wanglin.utils.RelativeDateFormat;

public class SignupUserForMyselfListActivity extends RecyclerListActivity {
    private String activityID=null;
    private BaseRecyclerAdapter adapter;
    private ArrayList<SignupUserBean> mSignupUserBeans=new ArrayList<SignupUserBean>();
    @Override
    protected RecyclerView.Adapter setAdapter() {
        adapter=new BaseRecyclerAdapter<SignupUserBean>(mSignupUserBeans, R.layout.item_userlist_vertical_formyself) {
            @Override
            public void onBind(final int position, final SignupUserBean data, ViewHolder holder) {
                ((SimpleDraweeView) holder.bind(R.id.iv_showphoto)).setImageURI(data.getHeadUrl());
                holder.setText(R.id.tv_username,data.getNickName());
                if(!TextUtils.isEmpty(data.getName())){
                    holder.setText(R.id.tv_username,data.getNickName());
                }else{
                    holder.setText(R.id.tv_username,"网邻用户");
                }
                if(!TextUtils.isEmpty(""+data.getCreateDate())){
                    holder.setText(R.id.tv_time, RelativeDateFormat.format(""+data.getCreateDate()));
                }else{
                    holder.setText(R.id.tv_time,"暂无时间");
                }
                if(!TextUtils.isEmpty(data.getName())){
                    holder.setText(R.id.tv_truename,data.getName());
                }else{
                    holder.setText(R.id.tv_truename,"暂无");
                }
                if(!TextUtils.isEmpty(data.getPhone())){
                    holder.setText(R.id.tv_phone,data.getPhone());
                }else{
                    holder.setText(R.id.tv_phone,"暂无");
                }
                holder.bind(R.id.tv_phone).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(data.getPhone()!=null){
                            boolean hasPermission = PermissionManager.request(SignupUserForMyselfListActivity.this, Manifest.permission.CALL_PHONE,"使用您的手机拨打电话",21);
                            if(hasPermission){
                                Intent intent = new Intent();
                                intent.setAction(Intent.ACTION_CALL);
                                intent.setData(Uri.parse("tel:" + data.getPhone()));
                                //开启系统拨号器
                                startActivity(intent);
                            }
                        }else {
                            showToast("获取活动电话号码失败！");
                        }

                    }
                });
            }
        };
        return adapter;
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("报名详情");
        activityID=getIntent().getStringExtra("activityID");
        getData();
    }
    private  void getData(){
        HttpManager.getSignupInfo(activityID).subscribe(new ResultDataSubscriber<ArrayList<SignupUserBean>>(this) {
            @Override
            public void onSuccess(String msg, ArrayList<SignupUserBean> signupUserBeen) {
                showToast(msg);
                if(signupUserBeen!=null&&signupUserBeen.size()!=0){
                    mSignupUserBeans.clear();
                    mSignupUserBeans.addAll(signupUserBeen);
                    adapter.notifyDataSetChanged();
                }else{
                }
            }
        });
    }

    @Override
    protected void pullDownRefresh() {
        super.pullDownRefresh();
        getData();
    }
}
